import axios, {AxiosRequestConfig} from "axios";
import {ElMessage} from "element-plus";

export interface MyResponseType<T = any> {
    status: number;
    message: string;
    data: T;
}

const errorHandler = (error: any) => {
    const status: number = error.response.status;
    ElMessage({
        message: "出现一点小问题",
        grouping: true,
        type: "error",
    });
    switch (status) {
        case 400:
            error.message = "请求错误";
            break;
        case 301:
            error.message = "未授权，请登录";
            break;
        case 403:
            error.message = "拒绝访问";
            break;
        case 404:
            error.message = `请求地址出错: ${error.response.config.url}`;
            break;
        case 408:
            error.message = "请求超时";
            break;
        case 500:
            error.message = "服务器内部错误";
            break;
        case 501:
            error.message = "服务未实现";
            break;
        case 502:
            error.message = "网关错误";
            break;
        case 503:
            error.message = "服务不可用";
            break;
        case 504:
            error.message = "网关超时";
            break;
        case 505:
            error.message = "HTTP版本不受支持";
            break;
        default:
            break;
    }
    return Promise.reject(error);
};
const instance = axios.create({
    baseURL: "https://netease-cloud-music-api-wheat-chi.vercel.app/",
    // baseURL: "http://localhost:4000/",
    withCredentials: true,  // 允许请求携带 cookie
    // validateStatus: function (status) {
    //     // 配置可以放行的状态码
    //     return status <= 503;
    // }
});
//拒绝重复请求相关
let url = "";
let params = {};
let data: any = null;
const compareFunction = (PreviousURL: string, PreviousParams: any, url: string, params: any) => {
    const compareParams = (PreviousParams: any, params: any) => {
        return (JSON.stringify(PreviousParams) === JSON.stringify(params));
    };
    // const testOne = {name: "lx", age: 18};
    // const testTwo = {name: "a", age: 19};
    // console.log(compareParams(testOne, testTwo));
    if (PreviousURL === "" && PreviousParams === {}) {
        return false;
    } else {
        return PreviousURL === url && compareParams(PreviousParams, params);
    }
};
const request = async <T = any>(config: AxiosRequestConfig): Promise<MyResponseType<T> | any> => {
    console.log(config);
    if (compareFunction(url, params, config.url as string, config.params)) {
        console.log("请求重复");
        return data;
    } else {
        url = config.url as string;
        params = config.params;
        try {
            data = await instance.request<MyResponseType<T> | any>(config);
            return data;
        } catch (error: any) {
            console.error(error.message);
            return {
                status: -1,
                message: error.message,
                data: null as any,
            };
        }
    }

};
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});
instance.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return errorHandler(error);
    // return Promise.reject(error)
});
export default request;
